﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Drawing;

namespace DxSnap
{
    public class TransparentPanel : Control
    {
        public TransparentPanel()
            : base()
        {
            SetStyle(ControlStyles.SupportsTransparentBackColor
              | ControlStyles.UserPaint
              | ControlStyles.AllPaintingInWmPaint
              | ControlStyles.Opaque, true);
            BackColor = Color.Transparent;
        }

        protected override void OnLocationChanged(EventArgs e)
        {
            // pick up the container's surface again. 
            Visible = false;
            Visible = true;
        }

        protected override CreateParams CreateParams
        {
            get
            {
                CreateParams cp = base.CreateParams;
                cp.ExStyle |= 0x00000020; //WS_EX_TRANSPARENT 
                return cp;
            }
        }

        private Image image;
        public Image Image
        {
            get
            {
                return image;
            }
            set
            {
                image = value;
                Size = image.Size;
            }
        }

        protected override void OnPaint(PaintEventArgs pe)
        {
            base.OnPaint(pe);
            if(image != null)
                pe.Graphics.DrawImage(image, 0, 0);
        }

    } 
}
